package com.sq.util.view;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sq.util.JsonUtil;
import com.sq.util.StringUtil;

/**
 * json视图.
 * 
 */
public class JsonView extends AbstractView {

    private static Logger log = LoggerFactory.getLogger(JsonView.class);

    public static final int SUCCESS = 200;

    public static final int FIAL = -1;

    private int code;

    private String msg;

    private Object data;

    public JsonView() {
    }

    public JsonView(Object data) {
        this(SUCCESS, data);
    }

    public JsonView(int code, Object data) {
        this.code = code;
        this.data = data;
        this.msg = "";
    }

    public JsonView(int code, Object data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    public static JsonView success() {
        return new JsonView(SUCCESS, null, "");
    }

    public static JsonView success(Object obj) {
        return new JsonView(SUCCESS, obj, "");
    }

    public static JsonView fail(String msg) {
        return new JsonView(FIAL, null, msg);
    }

    public static JsonView fail(int code, String msg) {
        return new JsonView(code, null, msg);
    }

    public int getcode() {
        return code;
    }

    public void setcode(int code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getmsg() {
        return msg;
    }

    public void setmsg(String msg) {
        this.msg = msg;
    }

    @Override
    public String getContentType() {
        return "text/plain; charset=UTF-8";
    }

    private Map<String, Object> getResult() {
        Map<String, Object> map = new HashMap<String, Object>(4, 1);
        map.put("code", this.code);
        map.put("msg", this.msg);
        map.put("data", this.data);
        return map;
    }

    /**
     * 
     * 1、默认返回json <br/>
     * URL:http://xxx/test/json.do<br/>
     * 返回:{"code":0,"msg":"","data":{xx}}<br/>
     * 
     * 2、自定义callback参数，返回jsonp<br/>
     * URL:http://xxx/test/json.do?callback=callback2 <br/>
     * 返回:callback2({"code":0,"msg":"","data":{xx}}); <br/>
     * 
     * 3、自定义var参数，返回script <br/>
     * URL:http://xxx/test/json.do?var=abc <br/>
     * 返回:var abc = {"code":0,"msg":"","data":{xx}} ; <br/>
     * 
     */
    @Override
    public String getBody(HttpServletRequest request) {
        try {
            return _getBody(request, JsonUtil.toJson(getResult()));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return _getBody(request, "{\"code\":" + FIAL + ",\"msg\":\"发生错误\",\"data\":null}");
        }
    }

    private String _getBody(HttpServletRequest request, String json) {
        String callback = request.getParameter("callback");
        if (StringUtil.isNotBlank(callback) && isValidCallback(callback)) {
            return callback + "(" + json + ");";
        }
        String var = request.getParameter("var");
        if (StringUtil.isNotBlank(var) && isValidCallback(var)) {
            return "var " + var + " = " + json + ";";
        }
        return json;
    }

    private static String VALID_REGEX = "^[a-zA-Z0-9_\\.]+$";

    private static boolean isValidCallback(String callback) {
        return callback.matches(VALID_REGEX);
    }

    @Override
    public String toString() {
        return "JsonView [code=" + code + ", msg=" + msg + ", data=" + data + "]";
    }

}
